package com.example.group_20.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.time.LocalDate;

/**
 * @author: 栋
 * @data: 2023/8/18
 * @descriptions:
 */
public class FileUtils {
    public static String SetName(int group_id,String category,String type){
        return "第"+group_id+"组+"+category+type;
    }

    public static String getPath(){
        return "D:/code/java/group_20/src/main/resources/static/";
    }

    public static String saveFile(MultipartFile file,String path,String category,int group_id)  {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        StringBuilder fileName=new StringBuilder(FileUtils.SetName(group_id,category,suffix));
        File dir=new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }
        File f=new File(path+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return path+fileName;

    }

    public static String getId(String lastId){
        StringBuilder id=new StringBuilder("F");
        Date date= Date.valueOf(LocalDate.now());
        String s = date.toString();
        String[] split = s.split("-");
        for (String s1 : split) {
            id.append(s1);
        }
        Integer end=1;
        if(lastId.length()!=0){
            if(lastId.startsWith(id.toString())){
                String sub=lastId.substring(9,14);
                end=Integer.valueOf(sub)+1;
            }
        }
        String endprefix = String.format("%05d",end);
        id.append(endprefix);
        String ids = id.toString();
        return ids;


    }
}
